<?php

namespace Home\Controller;

/**
 * 不用登录的公开对外页面
 * @modify AmosZhong 2016-01-07 创建
 */
class PublicPageController extends \Home\Controller\ParentController {

    protected function _initialize() {
        $this->validateUtil = new \Org\Util\Validate();
        $this->checkLanguage();
    }

    public function index() {
        $this->display();
    }

    public function book() {
        $param = I('get.');
        $hotelId = $param['h'];
        $idValidatePass = $this->validateUtil->validate($hotelId, 'notempty,id');
        if (!$idValidatePass) {
            redirect('/product.html');
        }
        $hotelBookLogic = A('Common/HotelBook', 'Logic');
        // 校验是否已经开通
        $isEnable = $hotelBookLogic->checkEnable($hotelId);
        if (!$isEnable) {
            redirect('/product.html');
        }

        $hotelDetail = $hotelBookLogic->getInfo($hotelId);
        $this->assign('hotelDetail', $hotelDetail);
        $this->assign('logoimageurl', empty($hotelDetail['logoimageurl'])?'/Public/uclbrt/image/meizhu.png':$hotelDetail['logoimageurl']);
        $this->display();
    }

    /**
     * 登录页面
     * @modify AmosZhong 2016-01-07
     */
    public function login() {
        $this->checkLogout();
        $this->display();
    }

    /**
     * 忘记密码页面
     * @modify AmosZhong 2016-01-07
     */
    public function forget() {
        //$this->checkLogout();
        $this->display();
    }

    /**
     * 重置密码页面
     * @modify songanwei 2016-01-12
     */
    public function reset() {
        //$this->checkLogout();
        $validator = new \Org\Util\Validate();
        $param = I('get.');
        $sessionId = $param['id'];
        $mobile = $param['mobile'];
        $idValidatePass = $validator->validate($sessionId, 'notempty,sessionId');

        if (!$idValidatePass) {
            $this->clearResetSession();
            redirect('/forgetPassword.html');
        }

        session_id($sessionId);
        $isMobile = $this->validateUtil->validate($mobile, 'notempty');
        $sessionMobile = session(C('RESET_PASSWORD_SEND_MOBILE'));

        if (empty($sessionMobile)) {
            $this->clearResetSession();
            redirect('/forgetPassword.html');
        }

        if ($isMobile) {
            // 手机已验证验证码超时情况,可直接跳转
            $validatePass = session(C('RESET_PASSWORD_VALIDATE_PASSWORD'));
            if ($sessionMobile != $mobile || empty($validatePass) || $validatePass == false) {
                $this->clearResetSession();
                redirect('/forgetPassword.html');
            }
        } else {
            $this->clearResetSession();
            redirect('/forgetPassword.html');
        }

        session(C('RESET_PASSWORD_VALIDATE_PASSWORD'), true);
        $this->assign('mobile', $sessionMobile);
        $this->display();
    }

    /**
     * 清空重置密码相关session
     * @modify trainyao 2015-10-13 创建
     */
    private function clearResetSession() {
        session(C('RESET_PASSWORD_VCODE'), NULL);
        session(C('RESET_PASSWORD_SEND_TO'), NULL);
        session(C('RESET_PASSWORD_SEND_TIME'), NULL);
        session(C('RESET_PASSWORD_VALIDATE_PASS'), NULL);
    }

    /**
     * 申请账号页面
     * @modify AmosZhong 2016-01-07
     */
    public function apply() {
        //获取区号时区数据
        $HotelLogic = A('Common/Hotel', 'Logic');
        $universalTimeData = $HotelLogic->universalTimeList();

        $this->assign('universalTimeList', $universalTimeData);
        // 获取系统货币
        $currencyLogic = A('Common/Currency', 'Logic');
        $currencyData = $currencyLogic->getBpCurrency();
        $this->assign('currencyInfo', $currencyData);

        $this->display();
    }
    
    /**
     * 申请账号页面 获取省份
     * @modify wenhui 2017-09-15
     */
    public function getProvinceList() {
        $param = I('post.');
        $validator = new \Org\Util\Validate();

        $universalTimeId = $param['universalTimeId'];
        $idValidatePass = $validator->validate($universalTimeId, 'notempty,notnull,id');

        //获取城市
        $HotelLogic = A('Common/Hotel', 'Logic');
        $provinceData = $HotelLogic->getProvinceList($universalTimeId);
        
        returnJson(200, 200, $provinceData);
    }
    
    /**
     * 申请账号页面 获取城市
     * @modify wenhui 2017-09-15
     */
    public function getCityList() {
        $param = I('post.');
        $validator = new \Org\Util\Validate();

        $provinceId = $param['provinceId'];
        $idValidatePass = $validator->validate($provinceId, 'notempty,notnull,id');

        //获取城市
        $HotelLogic = A('Common/Hotel', 'Logic');
        $cityListData = $HotelLogic->getCityList($provinceId);
        
        returnJson(200, 200, $cityListData);
    }
    
    /**
     * @description 退出
     * @modify songanwei 2016-01-08
     */
    public function logout() {
        session(null);
        cookie(C('COOKIE_SERVER_REMEMBER_ME_UUID'), null);
        cookie(C('COOKIE_SERVER_REMEMBER_ME_USSID'), null);
        cookie(C('COOKIE_SERVER_REMEMBER_ME_USSPS'), null);
        cookie(C('COOKIE_SERVER_REMEMBER_ME_PSTM'), null);
        redirect('/login.html');
    }
}
